package cn.ecut.serveltcontext;

import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;


@WebServlet("/getServletContext")
public class GetServletContextServlet extends HttpServlet  {
    @Override
    protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // 1、 通过 ServletConfig 对象进行获取
        ServletContext application = this.getServletConfig().getServletContext();
        System.out.println( application );
        // 2、可以通过 方法，直接获取
        application = this.getServletContext();
        System.out.println( application );
         // 3、可以通过 request 对象获取
        application = request.getServletContext() ;
        System.out.println( application );
        // 4、可以通过 HttpSession 获取到 ServletContext
        application = request.getSession().getServletContext() ;
        System.out.println( application );


        // 在其他的类中，进行获取
        Object o = application.getAttribute("currentDate") ;
        System.out.println( o );
    }
}
